package chatManagers;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TableCheckerChat extends HttpServlet {
    
    //----------------------------------------------------------------------------------------
    /** Servlet which verifies that a tables exist for the Expert Chat, and
     *  creates them if needed.
     */
    
    //doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        /*
        //The JSP document passes in which db & db table to use
        String db = request.getParameter("db");
        String qTable = "chatRooms";
        int db_present = 0;
        
        //Variables for the database connection
        DatabaseConnector con1 = null;
        DatabaseConnector con2 = null;
        Statement stmt1 = null;
        Statement stmt2 = null;
        Statement stmt3 = null;
        Statement stmt4 = null;
        ResultSet rs1 = null;
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            //Set the database connection
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            
            //String database = "jdbc:odbc:" + db;
            con1 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
            con2 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
            
            DatabaseMetaData dbmd = con1.getMetaData();
            
            // Specify the type of object; in this case we want tables
            String[] types = {"TABLE"};
            rs1 = dbmd.getTables(null, null, "%", types);
            
            // Get the table names
            while (rs1.next()) {
                // If the chatRooms table is present, increment db_present
                String tableName = rs1.getString(3);
                if(tableName.equals(qTable)) {
                    db_present++;
                }
            }
            rs1.close();
            con1.close();
            
        } catch (ClassNotFoundException e) {
            out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            out.println("SQLException caught in TableChecker: " + e.getMessage());
        } finally {
            try {
                if (con1 != null) {con1.close();}
                if (con2 != null) {con2.close();}
            } catch (SQLException ignored) {}
        }
        */
    }
}
